﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: cansik
 * Datum: 18.08.2010
 * Zeit: 08:20
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
using System;
using BlackXS.Info;
using TM;
using TM.API;
using TM.TMObjects;
using TM.Unofficial.Chat;
using TM.Unofficial.Chat.History;
using TM.Unofficial.PersonalMessageSystem;

namespace ThatsMeDotNetAPI_Test

{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.Title = "Thats-Me API Test";
			Console.WriteLine("Thats-Me API Test\n-----------------\n");

			BlackXS.Menu.ListMenu menu = new BlackXS.Menu.ListMenu();
			menu.AddEntry("Example1 -> Loading member example");
			menu.AddEntry("Example2 -> Login user and request status example");
			menu.AddEntry("Help");
			
			int mindex = menu.Show();
			
			Console.Clear();
			
			switch (mindex) {
				case 0:
					Example1();
					break;
					
				case 1:
					 Example2();
					break;
					
				case 2:
					Console.WriteLine("we are working on it!");
					break;
					
				default:
					Console.WriteLine("please choose a menu entry!");
					break;
			}

			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}

        /// <summary>
        /// Loading Example for a User
        /// </summary>
        public static void Example1()
        {
			string User;
			
			Console.Write("Username: ");
			User = Console.ReadLine();
			
            //we create a new TM object which provides the API
            ThatsMe tm = new ThatsMe();

            //We create a new member and load data into it through the API
            Member ObjectMember = tm.getMemberByNick(User);

            //and print it out
            Console.WriteLine(ObjectMember.Profile.ToString() + "\n" + ObjectMember.ID.ToString());
        }

        /// <summary>
        /// Login Example for a User
        /// Load Number of Alerts
        /// </summary>
        public static void Example2()
        {
			string User;
			string Password;
			
			Console.Write("Username: ");
			User = Console.ReadLine();
			
			Console.Write("Password: ");
			Password = BlackXS.Secure.PasswordHandler.ReadPassword();
			
            //we create a new User with the unofficial API
            TM.User ObjectMember = new TM.User(User);

            //we set the password for the user (for the login)
            ObjectMember.Password = Password;

            //we try to login
            if (ObjectMember.Login(false, true))
            {
                //on success we try to get information about the messages and the alerts
                Console.WriteLine("logged in!");
               	Console.WriteLine("you have " + ObjectMember.GetNumberOfMessages().ToString() + " messages!");
                Console.WriteLine("you have " + ObjectMember.GetNumberOfAlerts().ToString() +" alerts!");
                ReceivedMessage[] ms = ObjectMember.PersonalMessageInterface.GetMessages(1, "", "");
              
                foreach(ReceivedMessage m in ms)
                {
                	Console.WriteLine(m.ID + ": " + m.Sender + " -> " + m.Subject);
                }
      
                Console.WriteLine(ms[0].Text);
                
                ReceivedMessage[] alerts = ObjectMember.PersonalMessageInterface.GetAlerts(1, "", "");
              
                Console.WriteLine("\t-Alerts-");
                
                foreach(ReceivedMessage a in alerts)
                {
                	Console.WriteLine(a.ID + ": " + a.Sender + " -> " + a.Subject);
                }
                
                Console.WriteLine(alerts[0].Text);

                
                ObjectMember.Chat.LoadCurrentChats();
                int tmdotnetapichatid = ObjectMember.Chat.OpenChat("tmdotnetapi");
               	ChatWindow cw = ObjectMember.Chat.ChatWindows[tmdotnetapichatid];
               	
               	string messagehist = cw.ReadLastMessage();
               	ChatHistory history = new ChatHistory();
               	history.ParseChatXML(messagehist);
               	
               	Console.ForegroundColor = ConsoleColor.Green;
               	foreach(ChatMessage msg in history.Messages)
               	{
               		Console.WriteLine(msg.Time.ToString() + " " + msg.External.ToString() + ": " + msg.Text);
               	}
               	
               	ObjectMember.Chat.CloseChat(tmdotnetapichatid);
                //ObjectMember.Chat.CloseChat(tmdotnetapichatid);
                
                // ObjectMember.PersonalMessageInterface.SendPM(new PersonalMessage(ObjectMember.Nick, "TMDotNetAPI Test - " + AssemblyInfo.AssemblyVersion, "sent by " + AssemblyInfo.AssemblyTitle + "\nVersion: " + AssemblyInfo.AssemblyVersion));
            }
            else
            {
                Console.WriteLine("login failed!");
            }
        }
	}
}
